package com.mud.ultimate;

import java.util.Date;

import com.mud.ultimate.data.GameRecordDAO;
import com.mud.ultimate.dialogs.UploadHighScoreDialog;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

public class GameHandler {
	
	public static String USERNAME_KEY="userName";
	public static String SCORE_KEY="score";
	public static String BEST_SCORE_KEY="bestScore";
	public static String SUM_SCORE_KEY="sumScore";
	public static String AVG_SCORE_KEY="avgScore";
	public static String WORST_SCORE_KEY="worstScore";
	public static String GAME_COUNT_KEY="gameCount";
	public static String BEST_SCORE_TODAY_KEY="bestScoreToday";
	public static String SUM_SCORE_TODAY_KEY="sumScoreToday";
	public static String GAME_TIMESTAMP_KEY="gameTimestamp";
	public static String NEW_HIGH_SCORE_KEY="newHighScore";
	public static String NEW_GAME_SCORE_KEY="newGameScore";
	
	public static  int gameResult;
	public static  Context context;
	


	public static int getGameResult() {
		Log.e("JNITEST","Returning gameResult: "+gameResult);
		return gameResult;
	}

	public static void setGameResult(int gameResult) {
		Log.e("JNITEST","New gameResult: "+gameResult);
		GameHandler.gameResult = gameResult;
		
		SharedPreferences sharedPref = context.getSharedPreferences("GAME", Context.MODE_PRIVATE);
		
		int bestScore =sharedPref.getInt(BEST_SCORE_KEY, 0);
		int avgScore =sharedPref.getInt(AVG_SCORE_KEY, 0);
		int worstScore =sharedPref.getInt(WORST_SCORE_KEY, 0);
		int sumScore =sharedPref.getInt(SUM_SCORE_KEY, 0);
		int bestScoreToday =sharedPref.getInt(BEST_SCORE_TODAY_KEY, 0);
		int sumScoreToday =sharedPref.getInt(SUM_SCORE_TODAY_KEY, 0);
		int gameCount =sharedPref.getInt(GAME_COUNT_KEY, 0);
		long gameTimestamp =sharedPref.getLong(GAME_TIMESTAMP_KEY, 0);
		Date lastPlayedDate= new Date(gameTimestamp);
		Date now = new Date();
		
		
		SharedPreferences.Editor editor = sharedPref.edit();

		editor.putInt(SCORE_KEY, gameResult);
		editor.putBoolean(NEW_GAME_SCORE_KEY, true);
		
		
		if(gameResult>bestScore)
		{
			editor.putInt(BEST_SCORE_KEY, gameResult);	
			editor.putBoolean(NEW_HIGH_SCORE_KEY, true);
		}
		
		editor.putInt(AVG_SCORE_KEY, (avgScore*gameCount + gameResult)/(gameCount+1) );	
		
		if(gameResult<worstScore || worstScore==0)
		{
			editor.putInt(WORST_SCORE_KEY, gameResult);	
		}
		
		editor.putInt(SUM_SCORE_KEY, sumScore+gameResult);	
		
		if(lastPlayedDate.getDate()==now.getDate())
		{
			if(gameResult>bestScoreToday)
			{
				editor.putInt(BEST_SCORE_TODAY_KEY, gameResult);	
			}
			
			editor.putInt(SUM_SCORE_TODAY_KEY, sumScoreToday+gameResult);	
		}else
		{
			
			editor.putInt(BEST_SCORE_TODAY_KEY, gameResult);
			editor.putInt(SUM_SCORE_TODAY_KEY, gameResult);	
		}
		
		editor.putInt(GAME_COUNT_KEY, ++gameCount);	
		editor.putLong(GAME_TIMESTAMP_KEY, now.getTime());	
		
		
		editor.commit();
		
	}
	


}
